import { createRouter, createWebHistory } from "vue-router";
import store from '@/plugins/store'

const routes = [
    {path: '', name: 'auth', component: () => import('@/components/Auth.vue')},
    {path: '/home', name: 'home', meta: {requiresAuth: true}, component: () => import('@/components/Home.vue')},
    {path: '/admin', name: 'admin', component: () => import('@/components/Admin.vue')},
]

const router = createRouter({
    routes,
    history: createWebHistory(),
})


router.beforeEach((to, from, next) => {
    const isAuthenticated = store.state.token !== null

    if (to.meta.requiresAuth && !isAuthenticated) {
      next('')
    } else {
      next()
    }
  })

export default router
